// newHousePackage/pages/newHouseList/newHouseList.js

import { queryNewHouseByCondition } from "../../../service/house_service"
import { attentionAction } from "../../../service/attention_service"

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    requestData: {
      "bedRooms": "",
      "buildMold": "",
      "cityName": "",
      "elevator": "",
      "maxArea": null,
      "maxTotalPrice": null,
      "maxUnitPrice": null,
      "minArea": null,
      "minTotalPrice": null,
      "minUnitPrice": null,
      "ownership": "",
      "page": 0,
      "pageSize": 10,
      "storey": "",
      "subway": "",
      "tags": [],
      "toward": "",
      "use": "",
      "words": "",
      "years": "",
      "zone": ""
    },
    filterLsit: [],
    houseList: [],
    isBottomShow: false,
    isShowLoading: false,
    scrollRefreshStatus: true,
    tagName: "",
    communityNumber: "",
    communityName: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      communityNumber: options.number,
      communityName: options.name
    })

    wx.setNavigationBarTitle({
      title: this.data.communityName,
    })
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
  },

  //筛选组件确认返回:
  filterConfirm(e) {
    console.log("JJLog:筛选组件确认返回:" + JSON.stringify(e.detail));
    let fliterData = e.detail;
    for (let key in fliterData) {
      console.log("JJLog:当前的key是:" + key);
      if (fliterData[key] != null && key != "unitPrice" && key != "totalPrice" && key != "area") {
        let value = `requestData.${key}`
        this.setData({
          [value]: fliterData[key] == '不限' ? null : fliterData[key],
          'requestData.page': 0,
          houseList: [],
          isBottomData: false
        })

      }
      //筛选数据单价和总价处理 
      else if ((key == "unitPrice" || key == "totalPrice")) {
        if (key == "unitPrice") {
          let unitPriceStr = fliterData[key];
          if (unitPriceStr == '不限' || unitPriceStr == null) {
            this.setData({
              'requestData.minUnitPrice': null,
              'requestData.maxUnitPrice': null
            })
          } else {
            let arr = unitPriceStr.split("-");
            this.setData({
              'requestData.minUnitPrice': arr[0],
              'requestData.maxUnitPrice': arr[1]
            })
          }
        } else {
          let totalPriceStr = fliterData[key];
          if (totalPriceStr == '不限' || totalPriceStr == null) {
            this.setData({
              'requestData.minTotalPrice': null,
              'requestData.maxTotalPrice': null
            })
          } else {
            let arr = totalPriceStr.split("-");
            this.setData({
              'requestData.minTotalPrice': arr[0],
              'requestData.maxTotalPrice': arr[1]
            })
          }
        }
        this.setData({
          'requestData.page': 0,
          houseList: [],
          isBottomData: false
        })
      }
      //面积 
      else if (key == "area") {
        let areaStr = fliterData[key];
        console.log("JJLog:面积的时候:" + areaStr);
        if (areaStr == '不限' || areaStr == null) {
          this.setData({
            'requestData.minArea': null,
            'requestData.maxArea': null
          })
        } else {
          let arr = areaStr.split("-");
          this.setData({
            'requestData.minArea': arr[0],
            'requestData.maxArea': arr[1]
          })
        }
        this.setData({
          'requestData.page': 0,
          houseList: [],
          isBottomData: false
        })
      }
    }
    console.log("JJLog:筛选后的requestData:" + JSON.stringify(this.data.requestData));
    this.dealRequestData(this.data.requestData);


  },
  dealRequestData(data) {
    let tempArr = [];
    for (let key in data) {
      if (data[key] != null && data[key] && data[key] != "不限") {
        if (key == "houseStyle" || key == "saleStatus" || key == "subway" || key == "zone") {
          let obj = {
            key: key,
            value: data[key]
          }
          tempArr.push(obj);
        }
        if (key == "maxArea") {
          let obj = {
            key: key,
            value: data["minArea"] + "-" + data[key] + "㎡"
          }
          tempArr.push(obj);
        }
        if (key == "maxUnitPrice") {
          let obj = {
            key: key,
            value: ""
          }
          if (data[key] == 10000) {
            obj.value = "10000元/㎡以下"
          }
          if (data[key] == 15000) {
            obj.value = "10000-15000元/㎡"
          }
          if (data[key] == 20000) {
            obj.value = "15000-20000元/㎡"
          }
          if (data[key] == 25000) {
            obj.value = "20000-25000元/㎡"
          }
          if (data[key] == 30000) {
            obj.value = "25000-30000元/㎡"
          }
          if (data[key] > 30000) {
            obj.value = "30000元/㎡以上"
          }
          tempArr.push(obj);
        }
        if (key == "maxTotalPrice") {
          let obj = {
            key: key,
            value: ""
          }
          if (data[key] == 50) {
            obj.value = "50万以下"
          }
          if (data[key] == 100) {
            obj.value = "50-100万"
          }
          if (data[key] == 150) {
            obj.value = "100-150万"
          }
          if (data[key] == 200) {
            obj.value = "150-200万"
          }
          if (data[key] == 300) {
            obj.value = "200-300万"
          }
          if (data[key] > 300) {
            obj.value = "300万以上"
          }
          tempArr.push(obj);
        }
        if (key == "tags" && data[key] && data[key].length > 0) {
          let obj = {
            key: key,
            value: data[key][0]
          }
          tempArr.push(obj);
        }

      }
    }
    setTimeout(() => {
      this.setData({
        filterLsit: tempArr
      })
      console.log("JJLog:返回是:" + JSON.stringify(this.data.filterLsit));
    }, 800);
  },
  //清空所有条件;
  emptyFilterAction() {
    this.setData({
      filterLsit: [],
      'requestData.page': 0,
      houseList: [],
      isBottomData: false,
      "requestData.houseStyle": "",
      "requestData.maxArea": null,
      "requestData.maxTotalPrice": null,
      "requestData.maxUnitPrice": null,
      "requestData.minArea": null,
      "requestData.minTotalPrice": null,
      "requestData.minUnitPrice": null,
      "requestData.saleStatus": "",
      "requestData.subway": "",
      "requestData.tags": [this.data.tagName],
      "requestData.words": "",
      "requestData.zone": ""
    })



  },
  filterChangeMenu(e) {
    let show = e.detail.show;
    // console.log("JJLog:筛选组件展开或者收起:" + show);
  },
  //删除一个筛选条件:
  cancelFilterItemAction(e) {
    let item = e.currentTarget.dataset.item;
    let arr = this.data.filterLsit;
    let cancelIndex = 0;
    let key = item.key;
    arr.forEach((i, idx) => {
      if (i.key == item.key) {
        cancelIndex = idx
      }
    })
    arr.splice(cancelIndex, 1);
    let changeValue = `requestData.${key}`;
    this.setData({
      filterLsit: arr,
      'requestData.page': 0,
      houseList: [],
      isBottomData: false
    })
    if (key == "tags") {
      this.setData({
        [changeValue]: []
      })
    } else if (key == "maxUnitPrice") {
      this.setData({
        'requestData.maxUnitPrice': "",
        'requestData.minUnitPrice': "",
      })
    } else if (key == "maxTotalPrice") {
      this.setData({
        'requestData.maxTotalPrice': "",
        'requestData.minTotalPrice': "",
      })
    } else if (key == "maxArea") {
      this.setData({
        'requestData.maxArea': "",
        'requestData.minArea': ""
      })
    } else {
      this.setData({
        [changeValue]: ""
      })
    }


  },



  //滚动到底:
  scrolltolower() {
    if (this.data.isBottomShow) {
      //已经到底了就不加载了
      return;
    }
    let tempPage = this.data.requestData.page + 1;
    this.setData({
      'requestData.page': tempPage,
      isShowLoading: true,
    })

  },

  //scroll下拉刷新:
  refreshDownData() {
    console.log("JJLog:下拉刷新来了=====:");
    this.setData({
      scrollRefreshStatus: true,
      'requestData.page': 0,
      isBottomShow: false,
    })

  },

  //点赞操作:
  collectAction(e) {
    let { data, index } = e.detail;
    this.attentionAction(data, index);
  },

  //关注操作
  attentionAction(item, index) {
    let data = {
      collectType: 1,
      number: item.houseNumber
    }
    wx.showLoading();
    attentionAction(data).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        item.isCollect = !item.isCollect;
        //动态数组:
        let changeItem = 'houseList[' + index + '].isCollect';
        this.setData({
          [changeItem]: item.isCollect,
        })
        wx.showToast({
          title: item.isCollect ? "关注成功" : "取关成功",
          icon: "none"
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "操作失败",
        icon: "none"
      })
      console.error("JJLog:attentionAction", err);
    })
  },





  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },


})